package com.lisn.activityresultsapi

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.Toast
import com.lisn.activityresultsapi.net.NetworkChangeReceiver

class MainActivity : AppCompatActivity(), NetworkChangeReceiver.NetStateChangeObserver {
    private val TAG = "MainActivity"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // 注册
        NetworkChangeReceiver.registerReceiver(this);
        NetworkChangeReceiver.registerObserver(this);

        val bt_start = findViewById<Button>(R.id.bt_start)
        bt_start.setOnClickListener {

            val intent = Intent(this, FirstActivity::class.java)
            startActivity(intent)
        }

        val bt_start_main2 = findViewById<Button>(R.id.bt_start_java)
        bt_start_main2.setOnClickListener {

            val intent = Intent(this, FirstActivity2::class.java)
            startActivity(intent)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        //解除注册
        NetworkChangeReceiver.unRegisterReceiver(this);
        NetworkChangeReceiver.unRegisterObserver(this);
    }

    override fun onDisconnect() {
        Log.e(TAG, "onDisconnect: 网络中断 ")
        showToast("网络中断 ")
    }


    override fun onMobileConnect() {
        Log.e(TAG, "onMobileConnect: 移动网络连接 ")
        showToast("移动网络连接 ")
    }

    override fun onWifiConnect() {
        Log.e(TAG, "onWifiConnect: wifi 连接 ")
        showToast("wifi 连接 ")
    }

    private fun showToast(msg: String) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

}